//基础路径
var base = document.getElementById('testScript').getAttribute('base');
//当前用户
var userId=document.getElementById('testScript').getAttribute('userId');

// 时间戳转换成时间
function timestampToTime(cjsj) {
    var date = new Date(cjsj); //时间戳为10位需*1000，时间戳为13位的话不需乘1000
    var Y = date.getFullYear() + '-';
    var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
    var D = date.getDate() + ' ';
    var h = date.getHours() + ':';
    var m = date.getMinutes() + ':';
    var s = date.getSeconds();
    return Y + M + D;
}
var app=new Vue({
    el:'#app',
    data(){
        return{
            //显示的窗口
            show:'myCourses',
            //所有的学习过的课程
            courses:[],
            //最近学习的课程
            latelyCourse:{chapter:{},course:{}},
            //课程列表排序
            orderBy:'last_time desc',
            //课程等级条件
            filterLevel: '1',
            //是否注册教学方
            isTeacher:false
        }
    },
    computed: {
        sortItems: function() {
            return this.courses.sort();
        }
    },
    methods:{
        //切换显示内容
        showDiv:function (p) {
            this.show=p;
        },
        //退出
        logout:function () {
            console.log('logout');
        },
        //获取学习的课程
        getCourses:function (orderBy) {
            var _t=this;
            var data={
               orderBy: orderBy
            };
            axios.get(base + "/users/study", {
                params: data
            }).then(function (response) {
                if (response.data.code === 200) {
                    var data=response.data.data;
                    _t.courses=data;
                    _t.latelyCourse=data[0];
                }
            });
        },
        //格式化时间
        timestampToTime:function (time) {
            return timestampToTime(time);
        },
        //设置排序方式
        setOrderBy:function (orderBy) {
            this.orderBy=orderBy;
        },
        //查询是否是教学方
        getIsTeacher:function(){
            var _t=this;
            axios.get(base+"/teacher/"+userId)
                .then(function (response) {
                    if (response.data.code===200){
                        //已注册教学方
                        _t.isTeacher=userId;
                    }
                })
        }
    },
    created:function () {
        //加载学习的课程
        this.getCourses(this.orderBy);
        //查询是否注册教学方
        this.getIsTeacher();
    }
});